"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 自定义函数
Case Name   : 创建自定义函数后，gs_dump成功
Create At   : 2023/9/27
Owner       : @li-xin12345
Description :
    1、创建自定义函数
    2、gs_dump导出
    3、清理环境
Expect      :
    1、成功
    2、成功
    3、成功
History     :
"""

import os
import unittest

from yat.test import Node
from yat.test import macro

from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from testcase.utils.Common import Common


class RowCompressTest(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.pri_user = Node('PrimaryDbUser')
        self.constant = Constant()
        self.common = Common()
        self.func_name = 'func_bug_fix_0072'
        self.dump_path = os.path.join(os.path.dirname(
            macro.DB_INSTANCE_PATH), 'f_bug_fix_0072')
        self.default_param = self.common.show_param('uppercase_attribute_name')

    def test_row_compress(self):
        text = '-----step1:创建自定义函数;expect:成功-----'
        self.log.info(text)
        cmd = f'''drop function if exists {self.func_name};
        create function {self.func_name}(a int) returns int
        as \$\$
           select 2*a;
        \$\$ language sql;
        create or replace function 
        {self.func_name}(a int = {self.func_name}(1)) returns int
        as \$\$
           select 2*a;
        \$\$ language sql;
        '''
        res = self.pri_sh.execut_db_sql(cmd)
        self.log.info(res)
        self.assertIn(self.constant.DROP_FUNCTION_SUCCESS_MSG, res,
                      '执行失败：' + text)
        self.assertIn(self.constant.CREATE_FUNCTION_SUCCESS_MSG, res,
                      '执行失败：' + text)
        self.assertEqual(res.count(
            self.constant.CREATE_FUNCTION_SUCCESS_MSG), 2,
            '执行失败：' + text)

        text = '-----step2:gs_dump导出;expect:成功-----'
        self.log.info(text)
        dump_cmd = f"source {macro.DB_ENV_PATH};" \
                   f"gs_dump -p {self.pri_user.db_port} " \
                   f"{self.pri_user.db_name} -f {self.dump_path} -s"
        self.log.info(dump_cmd)
        dump_msg = self.pri_user.sh(dump_cmd).result()
        self.log.info(dump_msg)
        self.assertIn(self.constant.GS_DUMP_SUCCESS_MSG, dump_msg,
                      '执行失败:' + text)

    def tearDown(self):
        text = '-----step3:清理环境; expect:成功-----'
        self.log.info(text)
        cmd = f'drop function if exists {self.func_name};'
        drop_res = self.pri_sh.execut_db_sql(cmd)
        self.log.info(drop_res)

        cmd = f'rm -rf {self.dump_path};' \
              f'if [ -d {self.dump_path} ]; ' \
              f'then echo "exists"; else echo "not exists"; fi'
        rm_res = self.common.get_sh_result(self.pri_user, cmd)
        self.assertEqual('not exists', rm_res, '文件删除失败')
        self.assertIn(self.constant.DROP_FUNCTION_SUCCESS_MSG, drop_res,
                      '执行失败：' + text)

        self.log.info(f'-----{os.path.basename(__file__)} end-----')